package com.beidu.lottery.call.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 布隆过滤器配置类
 * <p>
 */
@Configuration
@Slf4j
public class RBloomFilterConfiguration {

    /**
     * 用户是否有抽奖次数查询缓存穿透布隆过滤器
     */
    @Bean
    public RBloomFilter<String> activityExistBloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("activityExistBloomFilter");
        // 预期插入数量
        long expectedInsertions = 1000L;
        // 错误比率
        double falseProbability = 0.01;
        bloomFilter.tryInit(expectedInsertions, falseProbability);
        log.info("activityExistBloomFilter init success");
        return bloomFilter;
    }

    /**
     * 用户是否有抽奖次数查询缓存穿透布隆过滤器
     */
    @Bean
    public RBloomFilter<String> lotteryNumQueryBloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("lotteryNumQueryBloomFilter");
        // 预期插入数量
        long expectedInsertions = 10000L;
        // 错误比率
        double falseProbability = 0.01;
        bloomFilter.tryInit(expectedInsertions, falseProbability);
        log.info("lotteryNumQueryBloomFilter init success");
        return bloomFilter;
    }
}
